#ifndef  __PROTOCOL_H__
#define  __PROTOCOL_H__

#include "stdint.h"
#include "string.h"
#include "myqueue.h"

#define    MAX_LEN              40
#define    PACKET_HEAD_LEN      16
#define    TIMEOUT_10S          1000

typedef struct CTOR {
	uint32_t default_inter;
	uint32_t length;
	uint32_t numbers;
	uint32_t interval;
}CTOR_t;

typedef struct DATA {
	uint32_t reissue;
	uint32_t interval;
	uint32_t re_cnt;
	uint32_t length;
	uint8_t data[0];
}DATA_t;

typedef struct PROTO {
	union {
		struct {
			uint32_t online :  1;
			uint32_t reissue:  1;
			uint32_t waitack:  1;
			uint32_t reserved: 29;
		}bit;
		uint32_t bits;
	}singles;
	uint8_t name[12];
	uint8_t send_frame[100];
	uint32_t ticks;
	uint32_t timeout;
	CTOR_t send_ctrol_t;
	RINGBUF_t* ack_queue;
	RINGBUF_t* recv_queue;
	RINGBUF_t* send_queue;
	uint8_t (*recvdata_calibration_fn)(uint8_t* buf, uint32_t len, uint8_t* s_buf, uint32_t* s_len);
	uint8_t (*recv_ack_data_check_fn)(struct PROTO* ptu, uint8_t* buf, uint32_t len);
	uint8_t (*recvdata_need_reply_ack_fn)(uint8_t* buf, uint32_t len, uint8_t* s_buf, uint32_t* s_len);

	void (*no_recv_ack_data_deal_fn)(struct PROTO* ptu, uint8_t* buf, uint32_t len);
	void (*send_ack_data_in_queue_fn)(struct PROTO* ptu, uint8_t* buf, uint32_t len);
	void (*send_data_fn)(uint8_t* buf, uint32_t len);
}PROTO_t, *PROTO_pt;


void protocol_timebase_computing(PROTO_pt ptu);
void protocol_send_data_out_queue(PROTO_pt ptu);
void protocol_recv_data_in_recv_queue(PROTO_pt ptu, uint8_t* buf, uint32_t len);
PROTO_pt protocol_stack_init(char* name, uint32_t r_num, uint32_t r_len, uint32_t s_num, uint32_t s_len, uint32_t a_num, uint32_t a_len);



#endif

